package com.qidian.mes.cal.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.cal.mapper.TeamMapper;
import com.qidian.mes.cal.service.TeamService;
import com.qidian.mes.pojo.Team;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    TeamMapper teamMapper;

    @Override
    //删除班组,同时删除班组与用户的关系
    public MESResult deleteById(String[] teamCodes) {
        return MESResult.ok(teamMapper.deleteById(teamCodes));
    }

    @Override
    //更改班组数据
    public MESResult update(Team team) {
        Team team1 = teamMapper.selectByTeamName(team.getTeamName());
        if (team1 != null && !team1.getTeamId().equals(team.getTeamId())) {
            return MESResult.duplicate("用户名重复");
        }
        return MESResult.ok(teamMapper.update(team));
    }

    @Override
    //根据id查询班组数据
    public MESResult selectByTeamId(Long teamId) {
        return MESResult.ok(teamMapper.selectByTeamId(teamId));
    }

    @Override
    public MESResult list(int page, int limit, Team team) {
        // 开启分页 在当前线程中执行下一条SQL的时候就会进行分页。
        PageHelper.startPage(page, limit);
        List<Team> teamList = teamMapper.list(team);
        // 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数）
        PageInfo<Team> pageInfo = new PageInfo<>(teamList);
        return MESResult.ok(pageInfo);
    }

    @Override
    //添加班组数据
    public MESResult save(Team team) {
        //判断用户名称是否重复
        if (team.getTeamName() != null && teamMapper.selectByTeamName(team.getTeamName()) != null) {
            return MESResult.duplicate("用户名重复");
        }
        return MESResult.ok(teamMapper.save(team));
    }

    @Override
    //查询全部班组数据
    public MESResult getList() {
        return MESResult.ok(teamMapper.getList());
    }

    @Override
    //根据班组编码查班组信息
    public MESResult selectByTeamCode(String teamCode) {
        return MESResult.ok(teamMapper.selectByTeamCode(teamCode));
    }
}
